#include "sprite.h"
#include "house.h"
using namespace std;


sprite::sprite(std::string filename, house* loc){
	home=loc;
	frames=1;
	curframe=0;
	SDL_Surface* loadedImage = NULL; 
	
	loadedImage = loc->loadImage( filename.c_str() ); 
	if( loadedImage != NULL ) { 
	sheet = SDL_DisplayFormat( loadedImage ); 	
	if (sheet!=NULL){
		Uint32 colorkey = SDL_MapRGB( sheet->format, 0xFF, 0, 0xFF );
		SDL_SetColorKey( sheet, SDL_SRCCOLORKEY, colorkey );
	}
	SDL_FreeSurface( loadedImage ); 
	} 

}
sprite::sprite(std::string filename,std::string sis, house* loc){
	home=loc;
	char temp[256];
	int tempframe=0;
	ifstream sisa(sis.c_str());
	sisa.getline(temp,256);
	frames=atoi(temp);
	for (int i=0; i<frames;i++){
		sisa.getline(temp,256);	
		int tx=atoi(temp);
		sisa.getline(temp,256);
		int ty=atoi(temp);
		sisa.getline(temp,256);
		int tw=atoi(temp);
		sisa.getline(temp,256);
		int th=atoi(temp);
		SDL_Rect to; 
		to.x = tx; 
		to.y = ty; 
		to.h = th;
		to.w = tw;
		clips[i]=to;
	}
	
	
	curframe=0;
	SDL_Surface* loadedImage = NULL; 
	 
	loadedImage = loc->loadImage( filename.c_str() ); 
	if( loadedImage != NULL ) { 
	sheet = SDL_DisplayFormat( loadedImage ); 	
	if (sheet!=NULL){
		Uint32 colorkey = SDL_MapRGB( sheet->format, 0xFF, 0, 0xFF );
		SDL_SetColorKey( sheet, SDL_SRCCOLORKEY, colorkey );
	}
	SDL_FreeSurface( loadedImage ); 
	} 

}


void sprite::SpriteDraw(int x, int y){
	if (frames==1){

	SDL_Rect offset; 
	offset.x = x; 
	offset.y = y; 
	SDL_BlitSurface( sheet, NULL, home->screen, &offset );



	}else{

SDL_Rect offset; 
	offset.x = x; 
	offset.y = y; 
	SDL_BlitSurface( sheet, &clips[curframe], home->screen, &offset );

	curframe++;
	curframe%=frames;
	}
return;
}
void sprite::SpriteDraw(int x, int y, int f){
	if (frames==1){

	SDL_Rect offset; 
	offset.x = x; 
	offset.y = y; 
	SDL_BlitSurface( sheet, NULL, home->screen, &offset );



	}else{
SDL_Rect offset; 
	offset.x = x; 
	offset.y = y; 
	SDL_BlitSurface( sheet, &clips[f], home->screen, &offset );


	}
return;
}